<template>
  <div class="SetDialog">
    <el-dialog
      :title="title"
      :visible="visible"
      @closed="$emit('clear')"
      @update:visible="$emit('update:visible', false)"
    >
      <el-form ref="form" :model="form">
        <el-form-item
          prop="name"
          label="角色名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="description"
          label="角色描述"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.description" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:visible', false)">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addRoles, editRoleById } from '@/api/setting'
export default {
  props: {
    visible: Boolean,
    form: Object
  },
  data() {
    return {
      //   visible: true,

      formLabelWidth: '120px',
      rules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },

  methods: {
    async submitForm() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.form.id) {
            const res = await editRoleById(this.form)
            // this.$message.success('修改成功')
          } else {
            const res = await addRoles(this.form)
          }

          //   console.log(res)
          // 通知父组件 刷新数据
          this.$emit('getDate')
          // 通知父组件包isShow给位false
          this.$emit('update:visible', false)
          this.$message.success(`${this.title}!yeah`)
        } else {
          this.$message.error('数据格式有误,请检查哦!^_^')
          return false
        }
      })
    }
  }
}
</script>

<style></style>
